package com.example.test.suppert.base.result;

import com.fasterxml.jackson.annotation.JsonInclude;

import java.io.Serializable;
import java.util.Objects;

/**
 * Created by lsl on 2021/2/25.
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
public class BaseResult<T> implements Serializable {
    private static final long serialVersionUID = -2733849641032268568L;
    public static final Integer SUCCESS_CODE = 200;
    public static final Integer BUSINESS_ERROR_CODE = 500;
    public static final Integer BAD_REQUEST_CODE = 400;
    protected Long id;
    protected Integer code;
    protected T data;
    protected String rpcLinkLogId;
    protected String subCode;
    protected String subMsg;
    protected String msg;
    protected Boolean success;

    public Long getId() {
        return id;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getRpcLinkLogId() {
        return rpcLinkLogId;
    }

    public String getSubCode() {
        return subCode;
    }

    public String getSubMsg() {
        return subMsg;
    }

    public String getMsg() {
        return msg;
    }

    public Boolean getSuccess() {
        return success;
    }

    public Boolean isSuccess() {
        return SUCCESS_CODE.equals(this.code);
    }

    public T getData() {
        return data;
    }

    public BaseResult() {
    }

    public static <T> BaseResult<T> success(T data) {
        return new SuccessResult(data);
    }

    public static <T> BaseResult<T> success() {
        return new SuccessResult();
    }

    public static <T> BaseResult<T> error(T data) {
        return new ErrorResult(data);
    }

    public static <T> BaseResult<T> error(T data, String code, String msg) {
        return new ErrorResult(data, code, msg);
    }

    public static <T> BaseResult<T> error(String code, String msg) {
        return new ErrorResult(code, msg);
    }

    public static <T> BaseResult<T> error(String msg) {
        return new ErrorResult(msg);
    }

    public static <T> BaseResult<T> error(ErrorCode errorCode) {
        if (Objects.isNull(errorCode)) {
            return new ErrorResult();
        }
        return new ErrorResult(String.valueOf(errorCode.getCode()), errorCode.getMsg());
    }
}
